import ConvertRoutes from './convertRoutes'
import router from '../../router'

/**
 * 动态添加路由
 * author:qlx
 */
const addRoutes = {
  state: {
    rootRoute:[],
    routeParam:[]//这里必须持久化参数,重新生成路由,因为里边的require方法没法持久化
  },
  mutations: {
    add_Routes(state, routeParam) {
      let routeList = [];
      //添加默认路由,新用户登陆可以显示首页
      routeList.push( {
            name: 'index',
            path: '/index',
            component: resolve => require(['@/components/index.vue'], resolve),
            meta: {
              title: '系统首页'
            }
      });
      let routeHideList=[];
      ConvertRoutes(routeList, routeHideList,routeParam); // 将后台的路由数据components转化成组件
      var rootRoute= [
            {
                    path: '/',
                    component: resolve => require(['@/components/Common/Home.vue'], resolve),
                    meta: { title: '自述文件' },
                    children:routeList
            },
            {
                name: 'tasklist',
                path: '/tasklist',
                component: resolve => require(['@/components/task/tasklist.vue'], resolve),
                meta: {
                  title: '任务列表'
                }
              },
            {
                 name: 'seedlist',
                 path: '/seedlist',
                 component: resolve => require(['@/components/seed/seedlist.vue'], resolve),
                 meta: {
                    title: '种子列表'
                 }
            },
            {
                 name: 'generator',
                 path: '/generatorlist',
                 component: resolve => require(['@/components/generator/generatorlist.vue'], resolve),
                 meta: {
                      title: '任务配置'
                 }
            },
            {
                 name: 'filter',
                 path: '/filter',
                 component: resolve => require(['@/components/filterconfig/filterlist.vue'], resolve),
                 meta: {
                       title: '爬取配置'
                 }
            },
            {
                 name: 'parse',
                 path: '/parse',
                 component: resolve => require(['@/components/parse/parselist.vue'], resolve),
                 meta: {
                       title: '爬取配置'
                 }
            },
            {
                 name: 'plug',
                 path: '/plug',
                 component: resolve => require(['@/components/parse/pluglist.vue'], resolve),
                 meta: {
                       title: '提取组件配置'
                 }
            },
              {
                name: 'searchinput',
                path: '/searchinput',
                component: resolve => require(['@/components/searchinput/searchinput.vue'], resolve),
                meta: {
                  title: '查询输入页'
                }
              },
              {
                name: 'tabpage',
                path: '/tabpage',
                component: resolve => require(['@/components/tabpage/tabpage.vue'], resolve),
                meta: {
                  title: '标签选项卡'
                }
              },
              {
                name: 'tablepage',
                path: '/tablepage',
                component: resolve => require(['@/components/tablepage/tablepage.vue'], resolve),
                meta: {
                  title: '综合表格页1'
                }
              }
              ,
              {
                name: 'formpage',
                path: '/formpage',
                component: resolve => require(['@/components/formpage/formpage.vue'], resolve),
                meta: {
                  title: '表单页'
                }
              }
              ,
              {
                name: 'treepage',
                path: '/treepage',
                component: resolve => require(['@/components/treepage/treepage.vue'], resolve),
                meta: {
                  title: '树组件页'
                }
              },
              {
                name: 'OrganizationTree',
                path: '/organizationTree',
                component: resolve => require(['@/components/admin/OrganizationTree.vue'], resolve),
                meta: { title: '机构管理' }
              },
              {
                name: 'userlist',
                path: '/userlist',
                component: resolve => require(['@/components/user/userlist.vue'], resolve),
                meta: { title: '用户管理' }
              },
              {
                name: 'SysAcl',
                path: '/sysAcl',
                component: resolve => require(['@/components/admin/SysAcl.vue'], resolve),
                meta: { title: '角色资源管理' }
              },
              {
                name: 'SysMenu',
                path: '/sysMenu',
                component: resolve => require(['@/components/admin/SysMenu.vue'], resolve),
                meta: { title: '菜单管理' }
              },
              {
                name: 'SysResource',
                path: '/sysResource',
                component: resolve => require(['@/components/admin/SysResource.vue'], resolve),
                meta: { title: '资源管理' }
              },
              {
                name: 'SysRole',
                path: '/sysRole',
                component: resolve => require(['@/components/admin/SysRole.vue'], resolve),
                meta: { title: '角色管理' }
              },
              {
                name: 'sysRoleMap',
                path: '/sysRoleMap',
                component: resolve => require(['@/components/admin/SysRoleMap.vue'], resolve),
                meta: { title: '角色权限管理' }
              },
              {
                name: 'msg',
                path: '/msg',
                component: resolve => require(['@/components/message/msg.vue'], resolve),
                meta: { title: '建议留言' }
              },
              {
                name: 'messageboard',
                path: '/messageboard',
                component: resolve => require(['@/components/tablepage/messageboard.vue'], resolve),
                meta: {
                  title: '增删改查'
                }
              },
              {
                name: 'messageboard2',
                path: '/messageboard2',
                component: resolve => require(['@/components/tablepage/messageboard2.vue'], resolve),
                meta: {
                  title: '增删改查二'
                }
              },
              {
                name: 'transition',
                path: '/transition',
                component: resolve => require(['@/components/flex/transition.vue'], resolve),
                meta: {
                  title: '左导航切换效果'
                }
              },
              {
                name:'detail',
                path: '/detail',
                component: resolve => require(['@/components/tablepage/detail.vue'], resolve),
                meta:{title:'详情页'}
              },
              {
                name:'ckeditor',
                path: '/ckeditor',
                component: resolve => require(['@/components/ckeditor/index.vue'], resolve),
                meta:{title:'文本编辑器'}
              }
];
      //添加新窗口打开的页面
      if(routeHideList&&routeHideList.length>0){
        rootRoute=rootRoute.concat(routeHideList);
      }
      //保存路由参数到store中
      state.routeParam=routeParam;
      state.rootRoute=rootRoute;
      //动态添加路由
      router.addRoutes(rootRoute)
      console.log('输出路由:',JSON.stringify(rootRoute));
    },
    add_Routes_Fresh(state){
       //if(state.routeParam&&state.routeParam.length>0){
        this.commit('add_Routes', state.routeParam)
      //}
    }
  },
  actions: {
    add_Routes({commit}, routeParam) {
      commit('add_Routes', routeParam)
    },
    add_Routes_Fresh({commit}) {
      commit('add_Routes_Fresh')
    }
  }
}
export default addRoutes
